package cn.com.taurus.controller.datatable;

import cn.com.taurus.bean.FundCompanyAndFundBean;
import cn.com.taurus.biz.AdminLogBiz;
import cn.com.taurus.biz.FundCompanyAndFundBeanBiz;
import cn.com.taurus.biz.FundCompanyBiz;
import cn.com.taurus.biz.MyPurseWebBeanBiz;
import cn.com.taurus.controller.user.LoginRegisterController;
import cn.com.taurus.controller.user.MyPurseController;
import cn.com.taurus.entity.AdminLog;
import cn.com.taurus.entity.Fund;
import cn.com.taurus.entity.FundCompany;
import cn.com.taurus.impl.FundCompanyImpl;
import cn.com.taurus.util.Log;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author zjw
 * @createTime 2021年6月28日
 * @remark 基金公司控制层
 * @version 1.0
 */
@Controller
public class FundCompanyController {
    @Autowired
    FundCompanyBiz fundCompanyBiz;
    @Autowired
    FundCompanyImpl fundCompanyImpl;
    @Autowired
    FundCompanyAndFundBeanBiz fundCompanyAndFundBeanBiz;
    @Autowired
    LoginRegisterController loginRegisterController;
    @Autowired
    MyPurseController myPurseController;

    @RequestMapping("/fundCompany")
    public String fundCompany() {
        return "datatable/fundCompany";
    }

    @RequestMapping("/updateFundCompany")
    @Log(operation = "修改基金公司信息",type = 2)
    public String updateFundCompany(@ModelAttribute FundCompany fundCompany){
        fundCompanyBiz.updateFundCompany(fundCompany);
        return "datatable/fundCompany";
    }

    /**
     * 模糊查询
     * @param pageNum 几条
     * @param pageSize 数量
     * @param fcId 公司编号
     * @param fcName 公司名称
     * @param fStartTime 公司成立时间
     * @param starFm 明星经理
     * @return
     */
    @ResponseBody
    @RequestMapping("/conditionSelectCompany")
    @Log(operation = "条件查询基金公司信息",type = 1)
    public HashMap conditionSelectCompany(Integer pageNum, Integer pageSize, String fcId, String fcName, String starFm,String fStartTime){
        System.out.println(fcId);
        System.out.println(fcName);
        System.out.println(starFm);
        System.out.println(fStartTime);
        HashMap<String, Object> map = new HashMap<>();
        // 开启分页支持
        PageHelper.startPage(pageNum,pageSize);
        List<FundCompany> rule = fundCompanyBiz.conditionSelect(fcId,fcName,starFm,fStartTime);
        PageInfo<FundCompany> pageInfo = new PageInfo<>(rule);
        System.out.println("数据："+pageInfo.getPages());
        map.put("code",0);
        map.put("msg","请求成功");
        //count：总数据条数
        map.put("count",pageInfo.getTotal());
        //data：数据
        map.put("data",pageInfo.getList());
        return map;
    }

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectFundCompany")
    public HashMap selectFundCompany(Integer pageNum,Integer pageSize){
        HashMap<String, Object> map = new HashMap<>();
        // 开启分页支持
        PageHelper.startPage(pageNum,pageSize);
        List<FundCompany> rule = fundCompanyBiz.selectAllFundCompany();
        PageInfo<FundCompany> pageInfo = new PageInfo<>(rule);
        System.out.println("数据："+pageInfo.getPages());
        map.put("code",0);
        map.put("msg","请求成功");
        //count：总数据条数
        map.put("count",pageInfo.getTotal());
        //data：数据
        map.put("data",pageInfo.getList());
        System.out.println(map);
        return map;
    }

    /**
     * 基金公司降序查询
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/fundCompanyTopByDesc")
    public ModelAndView fundCompanyTopByDesc(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        return new ModelAndView("major/Financechien2","fundCompanyTopByDescList",fundCompanyImpl.fundCompanyTopByDesc());
    }

    /**
     * 查询基金公司旗下基金
     * @param model 基金公司信息
     * @param request
     * @param session 基金信息
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/Anonymous/newFundCompany2")
    public String newFundCompany2(Model model, HttpServletRequest request, HttpSession session) throws Exception {
        FundCompany fundCompany =new FundCompany();
        //获取基金公司名字
        String fcName = request.getParameter("fcName");
        fundCompany.setFcName(fcName);
        model.addAttribute("myPurseData",myPurseController.getMyPurseWebBean());
        model.addAttribute("fundCompany",fundCompanyImpl.fundCompanyByFcName(fundCompany));
        //根据基金公司名字在基金公司连接表，查询该基金公司连接信息存入集合
        List<FundCompanyAndFundBean> list = fundCompanyAndFundBeanBiz.fundCompanyAndFundBeanList(new FundCompanyAndFundBean(fcName));
        //历遍基金公司集合 list
        session.setAttribute("list",list);
        for (FundCompanyAndFundBean fund:list) {
            System.err.println(fund.toString());
        }
        return "major/Financechient";
    }

}
